#include "pagetestconfig.h"
#include "clickedLabel.h"

extern void setView(QComboBox* target);

int channelMapping[6];

void configPage::getsendUdp_mut(char * ch,int flag)
{
    MUT_Send* mut_send=(MUT_Send*)ch;
    memset(mut_send, 0x00, sizeof(MUT_Send));

    memcpy(mut_send->de_name,"sanwei",6);

    if (flag == 0)                //复位命令
    {
        mut_send->udp_type='!';
    }
    else if (flag=='A')                //精度测试
    {
        mut_send->udp_type='A';

        if(sync_flag == 6)
        {
            mut_send->tbfs = '0';
        }
        else
        {
            mut_send->tbfs = sync_flag + '0';       /*同步方式  0,同步输出；1,电秒脉冲；2,光秒脉冲3,B码电信号；4,B码光信号；5,50Hz倍频*/
        }

        mut_send->ycdy = voltageStdIndex+'0';     /*一次电压  0-100，1-100√V */
        mut_send->ycdl = currentStdIndex+'0';     /*一次电流  0-1A, 1-5A     */
        mut_send->bcxz = dutIndex+'0';              /*被测选择  0-9-2, 1-FT3*/

        mut_send->tdlb[0] = channelMapping[0] + 1; //allways send, but only used for ft3
        mut_send->tdlb[1] = channelMapping[1] + 1;
        mut_send->tdlb[2] = channelMapping[2] + 1;
        mut_send->tdlb[3] = channelMapping[3] + 1;
        mut_send->tdlb[4] = channelMapping[4] + 1;
        mut_send->tdlb[5] = channelMapping[5] + 1;
    }
    else if(flag=='B')            //对时守时方法2
    {
        mut_send->udp_type='B';
        mut_send->tbfs = sync_flag+'0';

//        etc_send->ex[0]=itime >> 24 & 0xFF;
//        etc_send->ex[1]=itime >> 16 & 0xFF;
//        etc_send->ex[2]=itime >> 8 & 0xFF;
//        etc_send->ex[3]=itime  & 0xFF;
    }
    else                          //flag==c时，ft3 测试
    {
        mut_send->udp_type='C';

        if(sync_flag == 6)
        {
            mut_send->tbfs = '0';
        }
        else
        {
            mut_send->tbfs = sync_flag+'0';       /*同步方式  0,同步输出；1,电秒脉冲；2,光秒脉冲3,B码电信号；4,B码光信号；5,50Hz倍频*/
        }

        mut_send->ycdy = voltageStdIndex+'0';     /*一次电压  0-100，1-100√V */
        mut_send->ycdl = currentStdIndex+'0';     /*一次电流  0-1A, 1-5A     */
        mut_send->bcxz = dutIndex+'0';              /*被测选择  0-9-2, 1-FT3*/

        mut_send->tdlb[0] = channelMapping[0] + 1; //allways send, but only used for ft3
        mut_send->tdlb[1] = channelMapping[1] + 1;
        mut_send->tdlb[2] = channelMapping[2] + 1;
        mut_send->tdlb[3] = channelMapping[3] + 1;
        mut_send->tdlb[4] = channelMapping[4] + 1;
        mut_send->tdlb[5] = channelMapping[5] + 1;
    }
}

configPage::configPage(QWidget *parent)
    : QFrame(parent)
{
    setFrameStyle(QFrame::Panel | QFrame::Raised);
    setLineWidth(3);
    setStyleSheet("font-size:16px;");
    setStyleSheet("QFrame{background:#bbb;}"
                  ".QRadiobutton{min-width:50;min-height:20}");
    hide();

//    if(selectUnSync->isChecked())
//    {
//        mut_send->tbfs = '6';
//    }
//    else
//    {
//        mut_send->tbfs = cboTbfs->currentIndex()+'0';       /*同步方式  0,同步输出；1,电秒脉冲；2,光秒脉冲3,B码电信号；4,B码光信号；5,50Hz倍频*/
//    }
    QLabel *labSync = new QLabel("同步方式：");
    btnSync1 = new QRadioButton(tr("同步输出"));
    btnSync2 = new QRadioButton(tr("电秒脉冲"));
    btnSync3 = new QRadioButton(tr("光秒脉冲"));
    btnSync4 = new QRadioButton(tr("电B码"));
    btnSync5 = new QRadioButton(tr("光B码"));
    btnSync6 = new QRadioButton(tr("1588对时"));
    btnSync7 = new QRadioButton(tr("非同步方式"));
    btnSync1->click();

    labDelay = new QLabel("时延");
    inputDelay = new QLineEdit("1485");
    labDelayUnit = new QLabel("μs");

    labDelay->hide();
    inputDelay->hide();
    labDelayUnit->hide();

    connect(btnSync7, SIGNAL(toggled(bool)), this, SLOT(showDelay()));

    QHBoxLayout *layoutSync = new QHBoxLayout;
    //layoutSync->addWidget(labSync);
    layoutSync->addWidget(btnSync1);
    layoutSync->addWidget(btnSync2);
    layoutSync->addWidget(btnSync3);
    layoutSync->addWidget(btnSync4);
    layoutSync->addWidget(btnSync5);
    layoutSync->addWidget(btnSync6);
    layoutSync->addWidget(btnSync7);
    layoutSync->addWidget(labDelay);
    layoutSync->addWidget(inputDelay);
    layoutSync->addWidget(labDelayUnit);
    layoutSync->addStretch(1);

    QLabel *labVoltage = new QLabel("电压：");

    QLabel *labVoltagePre = new QLabel("额定一次");
    inputVoltagePre = new QLineEdit("110");
    inputVoltagePre->setFixedWidth(100);

    cboVoltageUnit = new QComboBox;
    cboVoltageUnit->setFixedWidth(100);
    cboVoltageUnit->addItem(tr(" KV"));
    cboVoltageUnit->addItem(tr("/√3KV"));
    setView(cboVoltageUnit);

    QHBoxLayout *layoutVoltage = new QHBoxLayout;
    layoutVoltage->setSpacing(10);
    layoutVoltage->addWidget(labVoltagePre);
    layoutVoltage->addWidget(inputVoltagePre);
    layoutVoltage->addWidget(cboVoltageUnit);
    layoutVoltage->addStretch(1);

    QLabel *labBlank1 = new QLabel("");
    QLabel *labVoltageDut = new QLabel("标准选择");
    cboVoltageStd = new QComboBox;
    cboVoltageStd->setFixedWidth(210);
    cboVoltageStd->addItem(tr("100 V"));
    cboVoltageStd->addItem(tr("100/√3 V"));
    cboVoltageStd->addItem(tr("模拟小电压"));
    setView(cboVoltageStd);

    connect(cboVoltageStd, SIGNAL(currentIndexChanged(int)), this, SLOT(showVSmall()));

    inputVoltageSmall = new QLineEdit("5");
    labVoltageSmall = new QLabel("V");
    inputVoltageSmall->hide();
    labVoltageSmall->hide();

    QHBoxLayout *layoutVoltage2 = new QHBoxLayout;
    layoutVoltage2->setSpacing(10);
    layoutVoltage2->addWidget(labVoltageDut);
    layoutVoltage2->addWidget(cboVoltageStd);
    layoutVoltage2->addWidget(inputVoltageSmall);
    layoutVoltage2->addWidget(labVoltageSmall);
    layoutVoltage2->addStretch(1);

    QLabel *labCurrent = new QLabel("电流：");

    QLabel *labCurrentPre = new QLabel("额定一次");
    inputCurrentPre = new QLineEdit("2000");
    inputCurrentPre->setFixedWidth(100);

    QLabel *labCurrentUnit = new QLabel("A");

    QHBoxLayout *layoutCurrent = new QHBoxLayout;
    layoutCurrent->setSpacing(10);
    layoutCurrent->addWidget(labCurrentPre);
    layoutCurrent->addWidget(inputCurrentPre);
    layoutCurrent->addWidget(labCurrentUnit);
    layoutCurrent->addStretch(1);

    QLabel *labBlank2 = new QLabel("");
    QLabel *labCurrentDut = new QLabel("标准选择");
    cboCurrentStd = new QComboBox;
    cboCurrentStd->setFixedWidth(210);
    cboCurrentStd->addItem(tr("1 A"));
    cboCurrentStd->addItem(tr("5 A"));
    cboCurrentStd->addItem(tr("模拟小电压"));
    setView(cboCurrentStd);

    connect(cboCurrentStd, SIGNAL(currentIndexChanged(int)), this, SLOT(showISmall()));

    inputCurrentSmall = new QLineEdit("5");
    labCurrentSmall = new QLabel("V");
    inputCurrentSmall->hide();
    labCurrentSmall->hide();

    QHBoxLayout *layoutCurrent2 = new QHBoxLayout;
    layoutCurrent2->setSpacing(10);
    layoutCurrent2->addWidget(labCurrentDut);
    layoutCurrent2->addWidget(cboCurrentStd);
    layoutCurrent2->addWidget(inputCurrentSmall);
    layoutCurrent2->addWidget(labCurrentSmall);
    layoutCurrent2->addStretch(1);

    QLabel *labDut = new QLabel("合并单元：");

    QLabel *labDutOutput = new QLabel("输出类型");
    cboDut = new QComboBox;
    cboDut->setFixedWidth(210);
    cboDut->addItem("IEC61850-9-2");
    cboDut->addItem("FT3");
    setView(cboDut);

    connect(cboDut, SIGNAL(currentIndexChanged(int)),this, SLOT(showDutCfg()));

    labMac = new QLabel("MAC地址");
    cboMacAddr = new QComboBox();
    cboMacAddr->setFixedWidth(200);
    cboMacAddr->addItem("None");
    setView(cboMacAddr);
    btnRefresh = new QPushButton("<= 扫描刷新");

    btnInstrument = new QRadioButton("测量通道");
    btnProtect = new QRadioButton("保护通道");
    QButtonGroup *grpFt3 = new QButtonGroup;
    grpFt3->addButton(btnInstrument);
    grpFt3->addButton(btnProtect);
    btnInstrument->click();

    QHBoxLayout *layoutDut = new QHBoxLayout;
    layoutDut->setSpacing(10);
    layoutDut->addWidget(labDutOutput);
    layoutDut->addWidget(cboDut);
    layoutDut->addWidget(labMac);
    layoutDut->addWidget(cboMacAddr);
    layoutDut->addWidget(btnRefresh);
    layoutDut->addWidget(btnInstrument);
    layoutDut->addWidget(btnProtect);
    layoutDut->addStretch(1);

    QLabel *labChannelMapping = new QLabel("通道映射：");

    QLabel *labChannelUa = new QLabel("Ua");
    QLabel *labChannelUb = new QLabel("Ub");
    QLabel *labChannelUc = new QLabel("Uc");
    QLabel *labChannelIa = new QLabel("Ia");
    QLabel *labChannelIb = new QLabel("Ib");
    QLabel *labChannelIc = new QLabel("Ic");

    cboChannelUa = new QComboBox;
    cboChannelUa->setFixedWidth(80);
    setView(cboChannelUa);
    cboChannelUb = new QComboBox;
    cboChannelUb->setFixedWidth(80);
    setView(cboChannelUb);
    cboChannelUc = new QComboBox;
    cboChannelUc->setFixedWidth(80);
    setView(cboChannelUc);
    cboChannelIa = new QComboBox;
    cboChannelIa->setFixedWidth(80);
    setView(cboChannelIa);
    cboChannelIb = new QComboBox;
    cboChannelIb->setFixedWidth(80);
    setView(cboChannelIb);
    cboChannelIc = new QComboBox;
    cboChannelIc->setFixedWidth(80);
    setView(cboChannelIc);

    for(int i=1; i<96; i++)
    {
        cboChannelUa->addItem(QString::number(i));
        cboChannelUb->addItem(QString::number(i));
        cboChannelUc->addItem(QString::number(i));
        cboChannelIa->addItem(QString::number(i));
        cboChannelIb->addItem(QString::number(i));
        cboChannelIc->addItem(QString::number(i));
    }
    cboChannelUa->setCurrentIndex(5);
    cboChannelUb->setCurrentIndex(5);
    cboChannelUc->setCurrentIndex(5);
    cboChannelIa->setCurrentIndex(8);
    cboChannelIb->setCurrentIndex(8);
    cboChannelIc->setCurrentIndex(8);

    QHBoxLayout *layoutChannelU = new QHBoxLayout;
    layoutChannelU->addWidget(labChannelUa);
    layoutChannelU->addWidget(cboChannelUa);
    layoutChannelU->addSpacing(30);
    layoutChannelU->addWidget(labChannelUb);
    layoutChannelU->addWidget(cboChannelUb);
    layoutChannelU->addSpacing(30);
    layoutChannelU->addWidget(labChannelUc);
    layoutChannelU->addWidget(cboChannelUc);
    layoutChannelU->addStretch(1);

    QHBoxLayout *layoutChannelI = new QHBoxLayout;
    layoutChannelI->addWidget(labChannelIa);
    layoutChannelI->addWidget(cboChannelIa);
    layoutChannelI->addSpacing(30);
    layoutChannelI->addWidget(labChannelIb);
    layoutChannelI->addWidget(cboChannelIb);
    layoutChannelI->addSpacing(30);
    layoutChannelI->addWidget(labChannelIc);
    layoutChannelI->addWidget(cboChannelIc);
    layoutChannelI->addStretch(1);

    QFormLayout *layoutConfig = new QFormLayout;
    layoutConfig->setContentsMargins(10,20,0,0);
    layoutConfig->addRow(labSync, layoutSync);
    layoutConfig->addRow(labVoltage, layoutVoltage);
    layoutConfig->addRow(labBlank1, layoutVoltage2);
    layoutConfig->addRow(labCurrent, layoutCurrent);
    layoutConfig->addRow(labBlank2, layoutCurrent2);
    layoutConfig->addRow(labDut, layoutDut);
    layoutConfig->addRow(labChannelMapping, layoutChannelU);
    layoutConfig->addRow(NULL, layoutChannelI);

    QPushButton *btnDoneConfig = new QPushButton("保存");
    QPushButton *btnCancel = new QPushButton("取消");

    QHBoxLayout *layoutBtn = new QHBoxLayout;
    layoutBtn->setContentsMargins(0,0,400,0);
    layoutBtn->addStretch(1);
    layoutBtn->addWidget(btnCancel);
    layoutBtn->addSpacing(10);
    layoutBtn->addWidget(btnDoneConfig);

    QVBoxLayout *layoutPageConfig = new QVBoxLayout;
    layoutPageConfig->addLayout(layoutConfig);
    layoutPageConfig->addLayout(layoutBtn);

    setLayout(layoutPageConfig);

    connect(btnCancel, SIGNAL(clicked(bool)), this, SLOT(hide()));
    connect(btnDoneConfig, SIGNAL(clicked(bool)), this, SLOT(configDone()));

    showDutCfg();

    loadCfg();
}

void configPage::showDelay()
{
    if(btnSync7->isChecked())
    {
        labDelay->show();
        inputDelay->show();
        labDelayUnit->show();
    }
    else
    {
        labDelay->hide();
        inputDelay->hide();
        labDelayUnit->hide();
    }
}

void configPage::configDone()
{
    updateChannelMapping();

    if( btnSync1->isChecked()) sync_flag=0;
    if( btnSync2->isChecked()) sync_flag=1;
    if( btnSync3->isChecked()) sync_flag=2;
    if( btnSync4->isChecked()) sync_flag=3;
    if( btnSync5->isChecked()) sync_flag=4;
    if( btnSync6->isChecked()) sync_flag=5;
    if( btnSync7->isChecked()) sync_flag=6;

    QString cboTbfs = QString::number(sync_flag);

    voltagePre = inputVoltagePre->text().toDouble();
    voltageStdIndex = cboVoltageStd->currentIndex();
    voltageStdUnitIndex = cboVoltageUnit->currentIndex();
    voltageSmall = inputVoltageSmall->text().toDouble();

    currentPre = inputCurrentPre->text().toDouble();
    currentStdIndex = cboCurrentStd->currentIndex();
    currentSmall = inputCurrentSmall->text().toDouble();

    dutIndex = cboDut->currentIndex();
    macAddr = cboMacAddr->currentText();

    QString digi_tdlb0 = QString::number(channelMapping[0]);
    QString digi_tdlb1 = QString::number(channelMapping[1]);
    QString digi_tdlb2 = QString::number(channelMapping[2]);
    QString digi_tdlb3 = QString::number(channelMapping[3]);
    QString digi_tdlb4 = QString::number(channelMapping[4]);
    QString digi_tdlb5 = QString::number(channelMapping[5]);

    QSettings *cfgWrite = new QSettings("c://model-2000/MuPara2.ini", QSettings::IniFormat);

    cfgWrite->setValue("/para/cboTbfs",cboTbfs);
    cfgWrite->setValue("/para/txtSourceVoltage",QString::number(voltagePre));
    cfgWrite->setValue("/para/cboSourceVoltage",QString::number(voltageStdIndex));
    cfgWrite->setValue("/para/cboSourceVoltageUnit",QString::number(voltageStdUnitIndex));
    cfgWrite->setValue("/para/voltageSmall",QString::number(voltageSmall));
    cfgWrite->setValue("/para/txtSourceCurrent",QString::number(currentPre));
    cfgWrite->setValue("/para/cboSourceCurrent",QString::number(currentStdIndex));
    cfgWrite->setValue("/para/currentSmall",QString::number(currentSmall));

    cfgWrite->setValue("/para/cboBcSelect",QString::number(dutIndex));
    cfgWrite->setValue("/para/macAddr",macAddr);

    cfgWrite->setValue("/tdlb/digi_tdlb0",digi_tdlb0);
    cfgWrite->setValue("/tdlb/digi_tdlb1",digi_tdlb1);
    cfgWrite->setValue("/tdlb/digi_tdlb2",digi_tdlb2);
    cfgWrite->setValue("/tdlb/digi_tdlb3",digi_tdlb3);
    cfgWrite->setValue("/tdlb/digi_tdlb4",digi_tdlb4);
    cfgWrite->setValue("/tdlb/digi_tdlb5",digi_tdlb5);

    delete cfgWrite;

    emit cfgDone();

    hide();
}

void configPage::loadCfg()
{
    sync_flag = 0;

    memset( channelMapping, 0x00, sizeof(channelMapping));

    voltageStdIndex = 0;
    voltageStdUnitIndex = 0;
    voltageSmall=5;

    currentStdIndex=0;
    currentSmall=5;

    voltagePre=110;
    currentPre=2000;

    dutIndex=0;
    macAddr="None";

    QString sfile = "c://model-2000//MuPara2.ini";

    if (QFile::exists(sfile))
    {
        QSettings *cfgRead = new QSettings(sfile, QSettings::IniFormat);

        sync_flag = cfgRead->value("/para/cboTbfs").toDouble();

        voltagePre = cfgRead->value("/para/txtSourceVoltage").toDouble();
        voltageStdIndex = cfgRead->value("/para/cboSourceVoltage").toInt();
        voltageStdUnitIndex = cfgRead->value("/para/cboSourceVoltageUnit").toInt();
        voltageSmall = cfgRead->value("/para/voltageSmall").toDouble();

        currentPre = cfgRead->value("/para/txtSourceCurrent").toDouble();
        currentStdIndex = cfgRead->value("/para/cboSourceCurrent").toInt();
        currentSmall = cfgRead->value("/para/currentSmall").toDouble();

        dutIndex = cfgRead->value("/para/cboBcSelect").toInt();
        macAddr = cfgRead->value("/para/macAddr").toString();

        channelMapping[0] = cfgRead->value("/tdlb/digi_tdlb0").toInt();
        channelMapping[1] = cfgRead->value("/tdlb/digi_tdlb1").toInt();
        channelMapping[2] = cfgRead->value("/tdlb/digi_tdlb2").toInt();
        channelMapping[3] = cfgRead->value("/tdlb/digi_tdlb3").toInt();
        channelMapping[4] = cfgRead->value("/tdlb/digi_tdlb4").toInt();
        channelMapping[5] = cfgRead->value("/tdlb/digi_tdlb5").toInt();

        delete cfgRead;
    }

    switch (sync_flag) {
    case 0:
        btnSync1->click();
        break;
    case 1:
        btnSync2->click();
        break;
    case 2:
        btnSync3->click();
        break;
    case 3:
        btnSync4->click();
        break;
    case 4:
        btnSync5->click();
        break;
    case 5:
        btnSync6->click();
        break;
    case 6:
        btnSync7->click();
        break;
    default:
        break;
    }

    cboChannelUa->setCurrentIndex(channelMapping[0]);
    cboChannelUb->setCurrentIndex(channelMapping[1]);
    cboChannelUc->setCurrentIndex(channelMapping[2]);

    cboChannelIa->setCurrentIndex(channelMapping[3]);
    cboChannelIb->setCurrentIndex(channelMapping[4]);
    cboChannelIc->setCurrentIndex(channelMapping[5]);

    cboVoltageStd->setCurrentIndex( voltageStdIndex);
    cboVoltageUnit->setCurrentIndex( voltageStdUnitIndex);
    inputVoltageSmall->setText(QString::number( voltageSmall));

    cboCurrentStd->setCurrentIndex( currentStdIndex);
    inputCurrentSmall->setText(QString::number( currentSmall));

    inputVoltagePre->setText(QString::number( voltagePre));
    inputCurrentPre->setText(QString::number( currentPre));

    cboDut->setCurrentIndex( dutIndex);
    cboMacAddr->setItemText(0, macAddr);
}

void configPage::showVSmall()
{
    if(cboVoltageStd->currentIndex() == 2)
    {
        inputVoltageSmall->show();
        labVoltageSmall->show();
    }
    else
    {
        inputVoltageSmall->hide();
        labVoltageSmall->hide();
    }
}

void configPage::showISmall()
{
    if(cboCurrentStd->currentIndex() == 2)
    {
        inputCurrentSmall->show();
        labVoltageSmall->show();
    }
    else
    {
        inputCurrentSmall->hide();
        labVoltageSmall->hide();
    }
}

void configPage::showDutCfg()
{
    labMac->hide();
    cboMacAddr->hide();
    btnRefresh->hide();

    btnInstrument->hide();
    btnProtect->hide();

    if(cboDut->currentIndex() == 0)
    {
        labMac->show();
        cboMacAddr->show();
        btnRefresh->show();
    }
    else
    {
        btnInstrument->show();
        btnProtect->show();
    }
}

void configPage::updateChannelMapping()
{
    channelMapping[0] = cboChannelUa->currentIndex();
    channelMapping[1] = cboChannelUb->currentIndex();
    channelMapping[2] = cboChannelUc->currentIndex();
    channelMapping[3] = cboChannelIa->currentIndex();
    channelMapping[4] = cboChannelIb->currentIndex();
    channelMapping[5] = cboChannelIc->currentIndex();

    qDebug()<<"digi_tdlb"<<channelMapping[0]<<channelMapping[1]<<channelMapping[2]<<channelMapping[3]<<channelMapping[4]<<channelMapping[5];
}
